﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Client_List.aspx.cs" Inherits="ncp.Web.cms.Client_List" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
    <link rel="stylesheet" type="text/css" href="../../themes/default/easyui.css" />
    <link rel="stylesheet" type="text/css" href="../../themes/icon.css" />
	<script type="text/javascript" src="../../js/jquery-1.7.1.min.js"></script>
	<script type="text/javascript" src="../../js/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="../../js/locale/easyui-lang-zh_CN.js"></script>
	<script type="text/javascript" src="../../../js/common.js"></script>
	<script src="/Plugin/My97DatePicker/WdatePicker.js" type="text/javascript"></script>
</head>
<body style="margin:0; padding:0px;">
    <form id="form1" runat="server">
    <div id="rightcontainer">
        <div class="toolbar" style="background-color:#efefef;font-size:12px;">
            <a href="#" class="easyui-linkbutton" plain="true" iconCls="icon-redo" onclick="onUpdateVip();">升级为VIP</a>
            <a href="#" class="easyui-linkbutton" plain="true" iconCls="icon-search" onclick="onSearchVip();">查看VIP历史记录</a>
            <a href="#" class="easyui-linkbutton" plain="true" iconCls="icon-edit" onclick="onEditFlags();">自定义属性</a>&nbsp;
            <span>查询条件:</span>
            <select id="ddlQuery">
                <option value="Client_Name">客户名称</option>
                <option value="Client_Account">客户帐号</option>
                <option value="User_Email">邮箱</option>
                <option value="User_CellPhone">手机</option>
                <option value="Points_Name">级别</option>
                <option value="User_Points">积分</option>
                <option value="User_RegsiterDate">注册时间</option>
            </select>
            <select name="ddlOperator" id="ddlOperator">
	            <option value="like">包含</option>
	            <option value="=">等于</option>
	            <option value=">">大于</option>
	            <option value="&lt;">小于</option>
            </select>
            <input name="txtQuery" type="text" id="txtQuery" style="width:80px;" />
	        <a href="#" class="easyui-linkbutton" plain="true" iconCls="icon-search" onclick="onSearch()">查询</a>
        </div>
        <div id="divright">    
        </div>
        </div>
	    <div id="divBox" class="easyui-window" title="升级为VIP" iconCls="icon-redo" resizable="true" collapsible="true" minimizable="false" maximizable="true" closable="true">
	        <p></p>
	        <table width="100%" cellpadding="3" cellspacing="0" border="0">
	            <tr>
                    <td>
                        <span style="padding:10px;">客户帐号：</span><input id="txtClient_Account" type="text" style="width:200px" maxlength="25" />
                    </td>
	            </tr>
	            <tr>
	                <td>
	                    <span style="padding:10px;">客户名称：</span><input id="txtClient_Name" type="text" style="width:200px" maxlength="100" />
	                </td>
	            </tr>
	            <tr>
	                <td>
	                    <span style="padding:10px;">开始时间：</span><input id="txtVip_StartDate" type="text" style="width:200px" readonly="readonly" onclick="WdatePicker()" />
	                </td>
	            </tr>
	            <tr>
	                <td>
	                    <span style="padding:10px;">到期时间：</span><input id="txtVip_EndDate" type="text" style="width:200px" readonly="readonly" onclick="WdatePicker()" />
	                </td>
	            </tr>
	            <tr>
	                <td>
	                    <span style="padding:10px;">备&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;注：</span><input id="txtVip_Remark" type="text" style="width:200px" maxlength="100" />
	                </td>
	            </tr>
	            <tr>
	                <td colspan="2" align="center">
	                    <a href="#" class="easyui-linkbutton" onclick="onSave();">确定</a>&nbsp;&nbsp;
	                    <a href="#" class="easyui-linkbutton" onclick="onClose();">关闭</a>
	                </td>
	            </tr>
	        </table>
		</div>
		<div id="divVipBox" class="easyui-window" title="查看VIP历史记录" iconCls="icon-redo" resizable="true" collapsible="true" minimizable="false" maximizable="true" closable="true">
		    <iframe id="p1" scrolling="yes" frameborder="0"  src="" style="width:100%;height:100%;"></iframe>
        </div>
   	    <div id="flagbox" class="mod-content">
	        <table style="margin-top: 10px;">
	            <tr>
                        <td class="mod-label"> <label for="newsTitle">自定义属性:</label></td>
                        <td align="left" id="flag">
            	            <input type="checkbox" name="flags" id="flagsh" value="h"/>头条[h]
            	            <input  type="checkbox" name="flags" id="flagsc" value="c"/>推荐[c]
            	            <input  type="checkbox" name="flags" id="flagsa" value="a"/>特荐[a]          
            	        </td>
                    </tr>
                    <tr>
                        <td colspan="2" align="center">
	                        <a href="#" class="easyui-linkbutton" onclick="onSetFlags();">确定</a>&nbsp;&nbsp;
	                        <a href="#" class="easyui-linkbutton" onclick="onClose();">关闭</a>
                        </td>
                    </tr>
	        </table>
	    </div>
	</form>
</body>
</html>
<script type="text/javascript">
    var pageobj = new Object();
    $(function() {
    $("#rightcontainer").css("height", $(window).height() + "px");
        initDialog();
        initPageObj();
        initDatagrid();
        LoadClientInfo();
        initPager();
    });

    //初始化对话框
    function initDialog() {
        $('#divBox').dialog({
            modal: true,
            shadow: true,
            width: 400,
            height: 250,
            padding: 10,
            closed: true
        });
        $('#divVipBox').dialog({
            title: '查看VIP历史记录',
            modal: true,
            shadow: true,
            width: 650,
            height: 350,
            padding: 10,
            closed: true
        });
        $('#flagbox').dialog({
            title: '自定义属性',
            modal: true,
            width: 370,
            height: 130,
            left: 100,
            top: 150,
            padding: 10,
            closed: true
        });
    }
    
    //初始化页码对象
    function initPageObj() {
        pageobj.currentpageindex = 1;
        pageobj.pagesize = 10;
        pageobj.sorting = "User_RegsiterDate";
        pageobj.sortdir = "ASC";
        pageobj.currentwhere = GetstrWhere();
        pageobj.recordcount = GetRecordCount();
    }

    //初始化表格
    function initDatagrid() {
        $('#divright').datagrid({
            width: 'auto',
            height: $(window).height() - 31,
            nowrap: true,
            striped: true,
            singleSelect: true,
            sortName: 'User_RegsiterDate',
            sortOrder: 'asc',
            remoteSort: false,
            idField: 'Client_Id',
            pagination: true,
            rownumbers: true,
            columns: [[
					{ field: 'Client_Account', title: '客户帐号', sortable: 'true', width: 80 },
					{ field: 'Client_Name', title: '客户名称', sortable: 'true', width: 120 },
					{ field: 'User_Email', title: '邮箱', sortable: 'true', width: 120, align: 'center' },
					{ field: 'User_CellPhone', title: '手机', sortable: 'true', width: 120, align: 'center' },
					{ field: 'User_Contact', title: '联系人', sortable: 'true', width: 80, align: 'center' },
					{ field: 'Points_Name', title: '级别', sortable: 'true', width: 60, align: 'center' },
					{ field: 'User_Points', title: '积分', sortable: 'true', width: 60, align: 'center' },
                    { field: 'User_Flag', title: '自定义属性', sortable: 'false', width: 100,
                        formatter: function(val, rec) {
                            var str = "<span style=\"color:red;\">";
                            if (val != null) {
                                if (val.indexOf('h') != -1) {
                                    str += "[头条]";
                                }
                                if (val.indexOf('c') != -1) {
                                    str += "[推荐]";
                                }
                                if (val.indexOf('f') != -1) {
                                    str += "[幻灯]";
                                }
                                if (val.indexOf('a') != -1) {
                                    str += "[特荐]";
                                }
                            }
                            str += "</span>";
                            return str;
                        }
                    },					
					{ field: 'isVip', title: '是否VIP', sortable: 'true', width: 60, align: 'center',
					    styler: function(value, row, index) {
					        if (value < 0) {
					            return 'background-color:#33CC00;color:#fff;font-weight:bold;';
					        }
					    },
					    formatter: function(value, rec) {
					        if (value / 1 < 0) {
					            return '是';
					        }
					        else {
					            return '否';
					        }
					    }
					},
					{ field: 'User_RegsiterDate', title: '注册时间', sortable: 'true', width: 120, align: 'center',
					    formatter: function(value, rec) {
					        return value.replace(" 00:00:00", "");
					    }
					},
					{ field: 'LastLoginTime', title: '上次登录时间', sortable: 'true', width: 120, align: 'center',
					    formatter: function(value, rec) {
					        return value.replace(" 00:00:00", "");
					    }
					},
					{ field: 'User_Status', title: '状态', sortable: 'true', width: 60, align: 'center',
					    styler: function(value, row, index) {
					        if (value == '0') {
					            return 'background-color:#6293BB;color:#fff;font-weight:bold;';
					        }
					    },
					    formatter: function(value, rec) {
					        if (value == '0') {
					            return '禁用';
					        }
					        else {
					            return '启用';
					        }
					    }
					},
					{ field: 'Client_Id', title: '操作', sortable: 'true', width: 60, align: 'center',
					    formatter: function(value, rec, rowIndex) {
					        if (rec.User_Status == '0')
					            return "<a href=\"#\" class=\"easyui-linkbutton\" onclick=\"onSeting(" + rowIndex + "," + value + ",1);\">启用</a>";
					        else
					            return "<a href=\"#\" class=\"easyui-linkbutton\" onclick=\"onSeting(" + rowIndex + "," + value + ",0);\">禁用</a>";
					    }
					}
				]]
        });
    }

    function initPager() {
        var p = $('#divright').datagrid('getPager');
        $(p).pagination({
            total: pageobj.recordcount,
            pageSize: pageobj.pagesize,
            pageList: [10, 15, 20, 25, 30],
            showRefresh: true,
            showPageList:true,
            beforePageText: '第',
            afterPageText: '页 共 {pages} 页',
            displayMsg: '当前显示 {from} - {to} 条记录 共 {total} 条记录',
            onSelectPage: function(pageNumber, pageSize) {
                $(this).pagination('loading');
                onShowPageNo(pageNumber, pageSize);
                $(this).pagination('loaded');
            },
            onBeforeRefresh: function(pageNumber, pageSize) {
                $(this).pagination('loading');
                onRefreshPageNo(pageNumber, pageSize);
                $(this).pagination('loaded');
            }
        });
    }
    
    //翻页
    function onShowPageNo(pageNumber, pageSize) {
        pageobj.pagesize = pageSize;
        pageobj.currentpageindex = pageNumber;
        LoadClientInfo();
        initPager();
        return false;
    }
    
    //刷新
    function onRefreshPageNo(pageNumber, pageSize) {
        initPageObj();
        return false;
    }
    
    //得到条件
    function GetstrWhere() {
        var response = ncp.Web.cms.Client_List.GetstrWhere($("#ddlQuery").val(), $("#ddlOperator").val(), $.trim($("#txtQuery").val()));
        if (response.value != null) {
            return response.value;
        }
        else
            return "";
    }

    //得到总记录数
    function GetRecordCount() {
        var response = ncp.Web.cms.Client_List.GetRecordCount(pageobj.currentwhere);
        if (response.value != null) {
            return response.value;
        }
        else
            return 0;
    }

    //查询
    function onSearch() {
        pageobj.currentpageindex = 1;
        pageobj.currentwhere = GetstrWhere();
        pageobj.recordcount = GetRecordCount();
        LoadClientInfo();
        initPager();
        return false;
    }

    //装载客户信息视图
    function LoadClientInfo() {
        var jsonStr = $.objectToJson(pageobj);
        var data = eval(ncp.Web.cms.Client_List.BindTable(jsonStr).value);
        $('#divright').datagrid('loadData', data);
    }

    //升级为VIP
    function onUpdateVip() {
        var selected = $('#divright').datagrid('getSelected');
        if (selected) {
            $('#divBox').dialog('setTitle', '升级为VIP');
            $('#divBox').dialog("open");
            $("#divBox").attr("client_id", selected.Client_Id);
            $("#txtClient_Account").val(selected.Client_Account);
            $("#txtClient_Name").val(selected.Client_Name);
            $("#txtVip_StartDate").val();
            $("#txtVip_EndDate").val();
            $("#txtVip_Remark").val();
        }
        else
            alert("请先选择一条记录！");
        return false;
    }

    //保存升级为VIP
    function onSave() {
        var obj = new Object();
        obj.client_id = $("#divBox").attr("client_id");
        obj.vip_startdate = $("#txtVip_StartDate").val();
        obj.vip_enddate = $("#txtVip_EndDate").val();
        obj.vip_remark = $("#txtVip_Remark").val();
        if (obj.vip_startdate == "") {
            alert("开始时间不能为空！");
            return false;
        }
        if (obj.vip_enddate == "") {
            alert("到期时间不能为空！");
            return false;
        }
        if (obj.vip_enddate < obj.vip_startdate) {
            alert("到期时间不能小于开始时间！");
            return false;
        }

        var jsonStr = $.objectToJson(obj);
        var response = ncp.Web.cms.Client_List.UpdateClientVip(jsonStr);
        if (response.value != null) {
            var jsonOBJ = eval(response.value)[0];
            if (jsonOBJ.isreload) {
                $('#divBox').dialog("close");
                var selected = $('#divright').datagrid('getSelected');
                var index = $('#divright').datagrid('getRowIndex', selected);
                $('#divright').datagrid('refreshRow', index);
            }
            alert(jsonOBJ.msg);
        }
        return false;
    }
    
    //启用/禁用客户
    function onSeting(rowIndex,Client_Id, User_Status) {
        $('#divright').datagrid('selectRow', rowIndex);
        var selected = $('#divright').datagrid('getSelected');
        if (User_Status == 1) var str = "启用";
        else var str = "禁用";
        if (confirm("您确定要" + str + "吗？")) {
            var response = ncp.Web.cms.Client_List.UpdateClientStatus(Client_Id, User_Status);
            if (response.value != null) {
                var jsonOBJ = eval(response.value)[0];
                selected.User_Status = User_Status;
                $('#divright').datagrid('refreshRow', rowIndex);
                alert(jsonOBJ.msg);
            }
        }
        return false;
    }

    //设置自定义属性
    function onEditFlags() {
        $("#flag input").removeAttr("checked");
        var selected = $('#divright').datagrid('getSelected');
        if (selected) {
            $('#flagbox').dialog('setTitle', '自定义属性');
            $('#flagbox').dialog("open");
            $("#flagbox").attr("client_id", selected.Client_Id);
            var flag = selected.User_Flag;
            if (flag != null) {
                if (flag.indexOf("h") != -1) {
                    $("#flagsh").attr("checked", true);
                }
                if (flag.indexOf("c") != -1) {
                    $("#flagsc").attr("checked", true);
                }
                if (flag.indexOf("f") != -1) {
                    $("#flagsf").attr("checked", true);
                }
                if (flag.indexOf("a") != -1) {
                    $("#flagsa").attr("checked", true);
                }
            }
        }
        else
            alert("请先选择一条记录！");
        return false;
    }
    
    //保存自定义属性
    function onSetFlags() {
        var client_id = $("#flagbox").attr("client_id");
        var flags = "";
        $("#flag input").each(function() {
            if ($(this).attr("checked")) {
                flags += "," + $(this).val();
            }
        });
        if (flags != "") {
            flags = flags.substring(1);
        }
        var response = ncp.Web.cms.Client_List.SetFlags(client_id, flags);
        if (response.value != "") {
            var jsonOBJ = eval(response.value)[0];
            alert(jsonOBJ.msg);
            if (jsonOBJ.isreload) {
                onClose();
                LoadClientInfo();
            }
        }
        return false;
    }
    
    //查看VIP记录
    function onSearchVip() {
        var selected = $('#divright').datagrid('getSelected');
        if (selected) {
            $('#divVipBox').dialog("open");
            $("#p1").attr("src", "Vip_List.aspx?client_id=" + selected.Client_Id);
        }
        else
            alert("请先选择一条记录！");
        return false;
    }
    
    //关闭客户信息对话框
    function onClose() {
        $('#divBox').window("close");
        $('#flagbox').window("close");
    }
</script>
